/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package terminal.ui;

import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import terminal.interfaces.ILineListener;

/**
 *
 * @author wro00113
 */
public class DocumentLineListener implements ILineListener
{
    public DocumentLineListener( Document document )
    {
        m_document = document;
    }
    
    @Override public void onNewLine( final String newLine )
    {
        if ( null != newLine )
        {
            appendLineToDocument( newLine );
        }
    }
    
    private void appendLineToDocument( final String newLine )
    {
        if ( null != m_document )
        {
            final int offset = m_document.getLength();
            final String toAdd = newLine + ( char )( CHAR_NL );

            try 
            {
                m_document.insertString( offset, toAdd, null );               
            }
            catch ( BadLocationException e )
            {
                System.out.println( e );
            }
        }        
    }
    
    private static final int CHAR_NL = '\n';
    private Document m_document;
}
